package com.haoshuang.zhouzhoubang.ui.dialog

import android.content.Context
import android.view.View

import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout

import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.config.URLConstants
import com.haoshuang.zhouzhoubang.model.bean.DetaiBean
import com.haoshuang.zhouzhoubang.model.bean.HomeBean
import com.haoshuang.zhouzhoubang.ui.page.help.detail.HelpDetailActivity
import com.haoshuang.zhouzhoubang.ui.page.order.receiveorder.detail.ServiceItemAdapter


import com.lxj.xpopup.core.BottomPopupView
import com.mufeng.libs.utils.SpannableStringUtils
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.visibleOrGone


class QdDialog4(
    context: Context,
    homeBean1: DetaiBean? = null,
    baoXianFee: String,
    private val onSubmit: () -> Unit
) : BottomPopupView(context) {
    override fun getImplLayoutId(): Int = R.layout.dialog_qd3
    var baoXianFee = baoXianFee
    var detailBean = homeBean1

    var zhankai = false

    override fun initPopupContent() {
        super.initPopupContent()

        var tvServiceNum = findViewById<TextView>(R.id.tv_service_num)
        var recyclerViewSeriviceItem = findViewById<RecyclerView>(R.id.recyclerViewSeriviceItem)
        var layoutLookmore = findViewById<LinearLayout>(R.id.layout_lookmore)
        var tvZhankai = findViewById<TextView>(R.id.tv_zhankai)
        var ivZhankai = findViewById<ImageView>(R.id.iv_zhankai)

        if (!detailBean?.order_goods.isNullOrEmpty()) {
            tvServiceNum?.text = "用户下单服务（共" + detailBean?.order_goods?.size + "项服务）"
            var serviceItemAdapter:ServiceItemAdapter?
            if (detailBean?.order_goods?.size!! >2){
                serviceItemAdapter   = ServiceItemAdapter(detailBean?.order_goods!!.subList(0,2))
                layoutLookmore.visibility = View.VISIBLE
            }else{
                serviceItemAdapter   = ServiceItemAdapter(detailBean?.order_goods!!)
                layoutLookmore.visibility = View.GONE
            }
            recyclerViewSeriviceItem.layoutManager = LinearLayoutManager(
                context,
                LinearLayoutManager.VERTICAL, false
            )
            recyclerViewSeriviceItem.adapter = serviceItemAdapter
          /*  layoutLookmore.click {
                serviceItemAdapter.submitList(detailBean?.order_goods!!)
                layoutLookmore.visibility = View.GONE
            }*/
            layoutLookmore.click {
                if (zhankai){
                    zhankai = false
                    tvZhankai.text = "查看更多"
                    ivZhankai.setImageResource(R.mipmap.icon_xl)
                    serviceItemAdapter.submitList(detailBean?.order_goods!!.subList(0,2))
                }else{
                    zhankai = true
                    tvZhankai.text = "收起"
                    ivZhankai.setImageResource(R.mipmap.icon_xl2)
                    serviceItemAdapter.submitList(detailBean?.order_goods!!)
                }

            }
        }


        var tvPrice = findViewById<TextView>(R.id.tvPrice)
        var tvGetPrice = findViewById<TextView>(R.id.tvGetPrice)
        var tvGb2 = findViewById<TextView>(R.id.tvGb)
        var checkboxTextView = findViewById<TextView>(R.id.checkboxText)
        var checkbox = findViewById<CheckBox>(R.id.checkbox)
//        val tvBaoXianFee = findViewById<TextView>(R.id.tvBaoXianFee)
        val content1 = context?.let {
            SpannableStringUtils(it, "我已阅读并同意《服务保障》和《师傅订单服务协议》")
                .first("我已阅读并同意")
                .onClick(checkboxTextView) {
                    checkbox.isChecked = !checkbox.isChecked
                }
                .first("《服务保障》")
                .textColor(R.color.c_3b76c2)
                .onClick(checkboxTextView) {
                    var url = "${URLConstants.baseUrl}/index/index/agreement?id=5"
                    context.startPage<HelpDetailActivity>("title" to "服务保障", "url" to url)
                }
                .first("《师傅订单服务协议》")
                .textColor(R.color.c_3b76c2)
                .onClick(checkboxTextView) {
                    var url = "${URLConstants.baseUrl}/index/index/agreement?id=6"
                    context.startPage<HelpDetailActivity>(
                        "title" to "师傅订单服务协议",
                        "url" to url
                    )
                }
        }
        checkboxTextView.text = content1
        tvPrice.text = detailBean?.pay_price
        tvGetPrice.text ="¥"+ detailBean?.pay_price
//        tvBaoXianFee.visibleOrGone(baoXianFee != null)
//        tvBaoXianFee.text = "服务完成后需从服务费中扣除${baoXianFee}元保险意外险"


        var tvCommit = findViewById<TextView>(R.id.tvCommit)

        tvGb2.setOnClickListener {

            dismiss()
        }
        tvCommit.setOnClickListener {

            if (!checkbox.isChecked) {
                toast("请勾选服务保证协议")
                return@setOnClickListener
            }
            dismiss()
            onSubmit()
        }
    }


}